package sz.szsmk.citizencard.mvp.presenter;

import com.google.gson.Gson;

import javax.inject.Inject;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.log.L;
import sz.szsmk.citizencard.model.network.GsdxNetApi;
import sz.szsmk.citizencard.model.network.bean.request.SearchViewRequest;
import sz.szsmk.citizencard.model.network.bean.response.SearchViewResponse;
import sz.szsmk.citizencard.mvp.contract.ISearchListContact;
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction;
import sz.szsmk.citizencard.retrofitrxjava.NetworkDateSources;
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult;
import sz.szsmk.citizencard.util.ACache;
import sz.szsmk.citizencard.util.Constant;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/9/16 22:06
 * description:
 */
public class SearchListPresenter extends IBasePresenter
        implements ISearchListContact.ISearchListPresenter<ISearchListContact.ISearchListView> {

    private NetworkDateSources mNetworkDateSource;
    public static final String LOGIN_TEST = "{\"telephone\":\"@1\",\"agent\":\"@2\"}";
    GsdxNetApi gsdxNetApi;

    @Inject
    public SearchListPresenter(GsdxNetApi gsdxNetApi) {
        this.gsdxNetApi = gsdxNetApi;

        this.mNetworkDateSource = NetworkDateSources.getInstance();
    }

    @Override
    public void getSearchList(String keyword, String value) {
        SearchViewRequest searchViewRequest = new SearchViewRequest();
        String access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token");
        searchViewRequest.setAccess_token(access_token);
        if (value.equals("familycard")) {
            searchViewRequest.setGroup_type(4);
        } else {
            searchViewRequest.setGroup_type(2);
        }
        searchViewRequest.setKeyword(keyword);
        String toJson = new Gson().toJson(searchViewRequest);
        L.e(Constant.TAG, "亲自娃娃卡：---->" + toJson);
        mNetworkDateSource.searchSpotAndHotelByKeyword(searchViewRequest, new SubscribeResult<SearchViewResponse>() {
            @Override
            protected void onOk(SearchViewResponse response) {
                if (mView != null) {
                    String json = new Gson().toJson(response);
                    L.e(Constant.TAG, "searchSpotAndHotelByKeyword---->" + json);
                    ((ISearchListContact.ISearchListView) mView).getSearchListSucc(response);
                }
            }

            @Override
            protected void onFailure(SearchViewResponse response) {
                super.onFailure(response);
                ((ISearchListContact.ISearchListView) mView).getSearchListFail();
            }
        }, new ErrorAction() {
            @Override
            public void onCall(Throwable throwable) {
                ((ISearchListContact.ISearchListView) mView).getSearchListFail();

            }
        });
    }
}
